package com.tang.exceptions;

import com.alibaba.fastjson.JSONObject;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Tang
 * @classname MyAuthenticationEntryPoint
 * @description [  ]
 * @date 2021/5/3 10:38
 */
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=UTF-8");
        JSONObject jsonObject = new JSONObject(){{
            put("msg", "没权限访问!");
        }};
        response.getWriter().println(jsonObject.toJSONString());
    }
}